 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=198 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Choose form validation style carefully
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='javascript,script,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Choose form validation style carefully</div>

<div class='main-body'>
 
<br>In web applications, validation of user input can be done with two
different tools :
<ul>
<li>
<a href="http://www.w3.org/TR/html4/interact/scripts.html">scripting</a>
code executed by the browser (javascript, for example)</li>

<li>
regular java code executed by the server, as part of the web application</li>
</ul>
Server validation :
<ul>
<li>cannot be disabled by a change in browser preferences</li>
<li>executes only after the <tt>SUBMIT</tt> button is pressed</li>
<li>validates all fields of the form as one unit</li>
<li>if an error is detected, a new web page is displayed, with an appropriate
error message</li>
</ul>

Browser validation :
<ul>
<li>can be disabled by a change in browser preferences</li>
<li>can be initiated in two ways : after the <tt>SUBMIT</tt> button is pressed,
or after an input field has changed</li>
<li>if executing after <tt>SUBMIT</tt> is pressed, the form's data is <tt>POST</tt>-ed
to the server only if validation is successful</li>
<li>if an error is detected, a pop-up dialog box is displayed</li>
</ul>

<i>Another important distinction between the two styles deals not with
their behavior, but with their testability</i>. Validation code on the
server can always be tested economically using tools such as <a href="TopicActiond5a5-2.html">JUnit</a>.
During the entire life of an application, such tests can be quickly repeated
using automated means, helping to identify possible errors caused by code
changes. Although tools such as <a href="http://httpunit.sourceforge.net/">HttpUnit</a> 
can test javascript behavior in web applications, such tests do not seem to be as simple 
to implement as JUnit tests on a <a href='TopicActiond08d-2.html'>Model Object</a>.
<p>Browser validation seems most useful for :
<ul>
<li>
low bandwidth environments</li>

<li>
forms with a large number of fields, each validated as it is input</li>
</ul>
If using scripting, should validation be done after <tt>SUBMIT</tt> is
pressed, or as each field is changed? If there are no dependencies between
fields, there is not much difference between the two styles. However, if
there are dependencies between fields, which is quite common, then the
validate-after-<tt>SUBMIT</tt> style is likely to be simpler to implement.
<p>Many prefer to treat server validation as mandatory and browser validation
as optional, either as a performance optimization or as the preferred user
experience. If both are implemented, then this will usually represent significant
code repetition.
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicActiond5a5-2.html?Id=33'>Use a testing framework (JUnit)</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='198'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=198 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
